import { FormRule, Icon, PrimaryTableCol, SelectProps, Tag } from 'tdesign-vue-next';

import { t } from '@/locales';

/**
 * 搜索表单项
 */
export const getSearchParams = () => [
  {
    label: t('pages.userPermission.name'),
    key: 'name',
    hide: false,
  },
  {
    label: t('pages.userPermission.nameEn'),
    key: 'nameEn',
    hide: false,
  },
  {
    label: t('pages.userPermission.type'),
    key: 'type',
    hide: true,
    defaultValue: 0,
  },
  {
    label: '',
    key: 'tree',
    hide: true,
    defaultValue: 1,
  },
];

/**
 * 表格列
 */
export const getColumns = (): PrimaryTableCol[] => [
  {
    title: t('pages.userPermission.name'),
    ellipsis: true,
    align: 'center',
    colKey: 'name',
  },
  {
    title: t('pages.userPermission.nameEn'),
    ellipsis: true,
    align: 'center',
    colKey: 'nameEn',
  },
  {
    title: t('pages.userPermission.code'),
    ellipsis: true,
    align: 'center',
    colKey: 'code',
  },
  {
    title: t('pages.userPermission.components'),
    ellipsis: true,
    align: 'center',
    colKey: 'components',
  },
  {
    title: t('pages.userPermission.outerChain'),
    ellipsis: true,
    align: 'center',
    colKey: 'outerChain',
    cell: (h, { row }) =>
      h(
        Tag,
        {
          theme: row.outerChain === 1 ? 'success' : 'danger',
          variant: 'light',
        },
        () => (row?.outerChain === 1 ? t('components.yesOrNo.yes') : t('components.yesOrNo.no')),
      ),
  },
  {
    title: t('pages.userPermission.icon'),
    ellipsis: true,
    align: 'center',
    colKey: 'icon',
    cell: (h, { row }) => h(Icon, { name: row.icon, size: '20' }),
  },
  {
    title: t('pages.userPermission.sort'),
    ellipsis: true,
    align: 'center',
    colKey: 'sort',
  },
  {
    title: t('pages.userPermission.remark'),
    ellipsis: true,
    align: 'center',
    colKey: 'remark',
  },
];

/**
 * 表单校验规则
 */
export const getRules = (): Record<string, FormRule[]> => ({
  type: [{ required: true, message: t('pages.userPermission.required.type'), type: 'error', trigger: 'blur' }],
  name: [{ required: true, message: t('pages.userPermission.required.name'), type: 'error', trigger: 'blur' }],
  nameEn: [{ required: true, message: t('pages.userPermission.required.nameEn'), type: 'error', trigger: 'blur' }],
  code: [{ required: true, message: t('pages.userPermission.required.code'), type: 'error', trigger: 'blur' }],
  path: [{ required: true, message: t('pages.userPermission.required.path'), type: 'error', trigger: 'blur' }],
  sort: [{ required: true, message: t('pages.userPermission.required.sort'), type: 'error', trigger: 'blur' }],
});

export const getTypeOptions = (): SelectProps['options'] => [
  {
    label: t('pages.userPermission.typeOptions.parent'),
    value: 0,
  },
  {
    label: t('pages.userPermission.typeOptions.children'),
    value: 1,
  },
  {
    label: t('pages.userPermission.typeOptions.perms'),
    value: 2,
  },
];

export const getRadioOptions = (): SelectProps['options'] => [
  {
    label: t('components.yesOrNo.no'),
    value: 0,
  },
  {
    label: t('components.yesOrNo.yes'),
    value: 1,
  },
];
